/* 
 * File:   mutex_threads.cpp
 * Author: tandaica0612
 *
 * Created on January 10, 2009, 8:47 PM
 */

#include <stdlib.h>
#include <pthread.h>
#include <iostream>
using namespace std;
/*
 * 
 */
int couter = 0;
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
//
void *increment(void* args){
    pthread_mutex_lock(&mymutex);
    for (int i =0;i < 5;i++){
        couter++;
        cout << (char*)args << " -- " << couter << endl;
        sleep(1);
    }
    pthread_mutex_unlock(&mymutex);
}
void *decrement(void *args){
    pthread_mutex_lock(&mymutex);
    for (int i =0;i < 5;i++){
        couter--;
        cout << (char*)args << " -- " << couter << endl;
        sleep(1);
    }
    pthread_mutex_unlock(&mymutex);
}
/*
int main(int argc, char** argv) {
    pthread_t threadone,threadtwo;
    pthread_create(&threadone,NULL,increment,(void *)"Thread One");
    pthread_create(&threadone,NULL,decrement,(void *)"Thread Two");
    pthread_join(threadone,NULL);
    pthread_join(threadtwo,NULL);
    return (EXIT_SUCCESS);
}
 **/
/*
Thread One -- 1
Thread One -- 2
Thread One -- 3
Thread One -- 4
Thread One -- 5
Thread Two -- 4
Thread Two -- 3
Thread Two -- 2
Thread Two -- 1
Thread Two -- 0
 **/
